﻿//using System;
//using NthDownload.Contracts;
//using System.Xml.Linq;
//using NthDownload.Common.Utils;
//using NthDownload.Contracts.Plugins.Downloads;

//namespace NthDownload.DownloadPlugins.LocalDownload
//{
//    public class LocalDownloadXmlSerializationService : IDownloadXmlSerializationService
//    {
//        private readonly string _pluginGUID;

//        public LocalDownloadXmlSerializationService(string pluginGUID)
//        {
//            _pluginGUID = pluginGUID;
//        }

//        public XElement Serialize(IDownload obj)
//        {
//            LocalDownload localDownload = obj as LocalDownload;

//            if (localDownload == null)
//            {
//                throw new Exception("Unexpected type, recieved '" + obj.GetType() + "' expected '" + typeof(LocalDownload) + "'.");
//            }

//            //
//            // Build XML
//            //
//            XElement rootElement = new XElement("Data");

//            rootElement.SetAttributeValue("GUID", localDownload.GUID);
//            rootElement.SetAttributeValue("CanResume", localDownload.CanResume);
//            rootElement.SetAttributeValue("CanQueue", localDownload.CanQueue);
//            rootElement.SetAttributeValue("HasSize", localDownload.HasSize);
//            rootElement.SetAttributeValue("IsInitialized", localDownload.IsInitialized);
//            rootElement.SetAttributeValue("Path", localDownload.Path);
//            rootElement.SetAttributeValue("DestinationFolder", localDownload.DestinationFolder);
//            rootElement.SetAttributeValue("DestinationPath", localDownload.DestinationPath);
//            rootElement.SetAttributeValue("Description", localDownload.Description);
//            rootElement.SetAttributeValue("Size", localDownload.Size);
//            rootElement.SetAttributeValue("FileName", localDownload.FileName);
//            rootElement.SetAttributeValue("Position", localDownload.Position);
//            rootElement.SetAttributeValue("ChunkSize", localDownload.ChunkSize);
//            rootElement.SetAttributeValue("DownloadedAmount", localDownload.DownloadedAmount);
//            rootElement.SetAttributeValue("DateCreated", localDownload.DateCreated);
//            rootElement.SetAttributeValue("DateCompleted", localDownload.DateCompleted);
//            rootElement.SetAttributeValue("TimeElapsed", localDownload.TimeElapsed);
//            rootElement.SetAttributeValue("Status", localDownload.Status);

//            XElement segmentsElement = new XElement("Segments");
//            rootElement.Add(segmentsElement);

//            foreach (DownloadSegment downloadSegment in localDownload.Segments)
//            {
//                XElement segmentElement = new XElement("Segment");

//                segmentElement.SetAttributeValue("StartPosition", downloadSegment.StartPosition);
//                segmentElement.SetAttributeValue("CurrentPosition", downloadSegment.CurrentPosition);
//                segmentElement.SetAttributeValue("EndPosition", downloadSegment.EndPosition);

//                segmentsElement.Add(segmentElement);
//            }

//            return rootElement;
//        }

//        public IDownload Deserialize(XElement data)
//        {
//            LocalDownload localDownload = new LocalDownload("");
//            Deserialize(data, localDownload);

//            return localDownload;
//        }

//        public void Deserialize(XElement data, IDownload obj)
//        {
//            if (data == null)
//            {
//                throw new ArgumentNullException("data");
//            }

//            if (obj == null)
//            {
//                throw new ArgumentNullException("obj");
//            }

//            LocalDownload localDownload = obj as LocalDownload;

//            if (localDownload == null)
//            {
//                throw new Exception("'" + typeof(LocalDownload) + "' expected '" + obj.GetType() + "' recieved when attempting to deserialize download." );
//            }

//            localDownload.GUID = (string)data.Attribute("GUID") ?? "";
//            localDownload.CanResume = (bool?)data.Attribute("CanResume") ?? true;
//            localDownload.CanQueue = (bool?)data.Attribute("CanQueue") ?? true;
//            localDownload.HasSize = (bool?)data.Attribute("HasSize") ?? true;
//            localDownload.IsInitialized = (bool?)data.Attribute("IsInitialized") ?? false;
//            localDownload.Path = (string)data.Attribute("Path") ?? "";
//            localDownload.DestinationFolder = (string)data.Attribute("DestinationFolder") ?? "";
//            localDownload.DestinationPath = (string)data.Attribute("DestinationPath") ?? "";
//            localDownload.Description = (string)data.Attribute("Description") ?? "";
//            localDownload.Size = (long?)data.Attribute("Size") ?? 0;
//            localDownload.FileName = (string)data.Attribute("FileName") ?? "";
//            localDownload.Position = (int?)data.Attribute("Position") ?? 0;
//            localDownload.ChunkSize = (int?)data.Attribute("ChunkSize") ?? 0;
//            localDownload.DownloadedAmount = (long?)data.Attribute("DownloadedAmount") ?? 0;
//            localDownload.DateCreated = (DateTime?)data.Attribute("DateCreated") ?? DateTime.Now;
//            localDownload.DateCompleted = (DateTime?)data.Attribute("DateCompleted") ?? DateTime.Now;
//            localDownload.TimeElapsed = (TimeSpan?)data.Attribute("TimeElapsed") ?? new TimeSpan(0);

//            localDownload.ParentGUID = _pluginGUID;

//            DownloadStatuses downloadStatus;
//            Enum.TryParse((string)data.Attribute("Status") ?? "", out downloadStatus);
//            localDownload.Status = downloadStatus;

//            localDownload.Segments.Clear();
//            XElement segmentsElement = XmlUtils.GetDecendant(data, "Segments");
//            foreach (XElement segmentElement in segmentsElement.Descendants("Segment"))
//            {
//                DownloadSegment downloadSegment = new DownloadSegment();

//                downloadSegment.StartPosition = (long?)segmentElement.Attribute("StartPosition") ?? 0;
//                downloadSegment.CurrentPosition = (long?)segmentElement.Attribute("CurrentPosition") ?? 0;
//                downloadSegment.EndPosition = (long?)segmentElement.Attribute("EndPosition") ?? 0;

//                localDownload.Segments.Add(downloadSegment);
//            }
//        }
//    }
//}
